package divinity.user
{
	
	import divinity.events.UserEvent;
	import divinity.utils.log.Log;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	
	public class User
	{
		private static var _data:Object;
		private static var _img:Bitmap=new Bitmap(new BitmapData(100,200,true,0));;	
		private static var _logged:Boolean=false;
		private static var user_type:String;
		private static var disp:EventDispatcher;
		private static var _log:String="[LOG]";
		public function User()
		{
			
		}
		/**
		 * Login user
		 * @param type Type of login account
		 * */
		public static function login(type:String)
		{			
			trace("[USER] logging in")
			_log+="LOGGING IN";
			user_type=type;
			_logged=true;
			dispatchEvent(new UserEvent(UserEvent.LOGGING));
			if(user_type==UserTypes.FACEBOOK)
			{
				ExternalInterface.call("getUserInfo");
			}
		}
		/**
		 * Logout user
		 * */
		public static function logout()
		{
			trace("[USER] logged out");
			_log+="LOGGED OUT";
			_logged=false;
			_data=null;	
			_img=new Bitmap(new BitmapData(100,200,true,0));
			dispatchEvent(new UserEvent(UserEvent.STATUS));
		}
		
		public static function userData(data:Object)
		{			
			_log+="USER INFO";
			_data=data;
			if(user_type==UserTypes.FACEBOOK)
			{
				
				ExternalInterface.call("getPic");
				_log+="GET PIC ";
			}
			//getImage();
		}
		
		public static function setImage(link:String)
		{		
			_log+="USER IMAGE";
			link=String(link).replace("https://","http://");
			_img=new Bitmap();
			//ExternalInterface.call("makeAlert","ucitavam sliku "+link);
			var req:URLRequest=new URLRequest("http://uskoci.com/games/stemalica/php/image.php?img="+link);
			var ldr:Loader=new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,imgLoaded);
			ldr.load(req);
			
		}
		private static function imgLoaded(e:Event)
		{
			//ExternalInterface.call("makeAlert","slika ucitana ");
			//trace("imgae loaded",e.target.content.bitmapData.width,e.target.content.bitmapData.height);	
			_img.bitmapData=e.target.content.bitmapData;
			_img.smoothing=true;
			_log+="logged in";
			dispatchEvent(new UserEvent(UserEvent.STATUS));
			
		}
		/***
		 * 
		 * 
		 * 
		 * 
		 * */
		public static function get DATA():Object
		{
			return _data;
		}
		public static function get AVATAR():Bitmap
		{
			trace(_img);
			return _img;
		}
		public static function get NAME():String
		{
			if(_logged)
			{
				return _data.first_name;
			}
			else
			{
				return "Some";
			}
		}
		public static function get LASTNAME():String
		{
			if(_logged)
			{
				return _data.last_name;
			}
			else
			{
				return "User";
			}
		}
		public static function get SEX():String
		{
			if(_logged)
			{
				return _data.gender
			}
			else
			{
				return "yes please";
			}
		}
		public static function get UID():int
		{
			var id:Number=0;
			if(user_type==UserTypes.FACEBOOK)
			{
				id=_data.id;
			}
			return id;
		}
		public static function get LOGGED():Boolean			
		{
			return _logged;
		}
		/**
		 * 
		 * STATIC DISPATCHER FUNCTIONS
		 * 
		 * */
		public static function addEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false, p_priority:int=0, p_useWeakReference:Boolean=false):void 
		{
			if (disp == null) 
			{ 
				disp = new EventDispatcher(); 
			}
			
			disp.addEventListener(p_type, p_listener, p_useCapture, p_priority, p_useWeakReference);
		}
		public static function removeEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false):void 
		{
			if (disp == null)
			{ 
				return; 
			}			
			disp.removeEventListener(p_type, p_listener, p_useCapture);
		}
		public static function dispatchEvent(p_event:Event):void 
		{
			
			if (disp == null) 
			{ 
				return; 
			}
			disp.dispatchEvent(p_event);
		}
		//
		//
		public static function get LOG():String
		{
			return _log;	
		}
		
	}
}